/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.editura.common.swing.imageviewer;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.TransferHandler;

/**
 *
 * @author kschmidtner
 */
public class DropTextOnImageTransferHandler extends TransferHandler {

    @Override
    public boolean canImport(TransferSupport support) {
        if (!support.isDrop()) {
            return false;
        }
        support.setShowDropLocation(true);
        support.setDropAction(COPY);
        return true;
    }

    @Override
    public boolean importData(TransferSupport support) {
        String text = null;
        if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                text = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException ex) {
                Logger.getLogger(DropTextOnImageTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DropTextOnImageTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
            }

            ImageCanvas imgC = (ImageCanvas) support.getComponent();
            Point pCanvas = imgC.getMousePosition();
            float zoomRatio = imgC.getZoomRatio();

            float newX = (pCanvas.x * zoomRatio) + imgC.getOffX();
            float newY = (pCanvas.y * zoomRatio) + imgC.getOffY();
            
            imgC.getTextBoxes().add(new TextBoxOnImage(text, (int) newX, (int)newY));
            imgC.repaint();

            //System.out.println("Hier kommt der String: " + text);
        }
        return true;
    }
}
